﻿using System.ServiceModel;
using Budget.Silver.BalanceServiceReference;
using Budget.Silver.IncomeServiceReference;
using Budget.Silver.OutcomeServiceReference;
using Budget.Silver.StatisticsServiceReference;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;

namespace Budget.Silver.Core.Locator.Installers
{
    public class WcfInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            const string incomeAddressFormat = "{0}/Services/IncomeService.svc",
                         outcomeAddressFormat = "{0}/Services/OutcomeService.svc",
                         balanceAddressFormat = "{0}/Services/BalanceService.svc",
                         statisticsAddressFormat = "{0}/Services/StatisticsService.svc";

            string host;
#if DEBUG
            host = "http://localhost/BudgetDev";
#else
            host = "http://budget.somee.com";
#endif
            var incomeEndpoint = new EndpointAddress(string.Format(incomeAddressFormat, host));
            var incomeBinding = new BasicHttpBinding {MaxReceivedMessageSize = 100000};
            var incomeService = new IncomeServiceClient(incomeBinding, incomeEndpoint);

            var outcomeEndpoint = new EndpointAddress(string.Format(outcomeAddressFormat, host));
            var outcomeBinding = new BasicHttpBinding { MaxReceivedMessageSize = 100000 };
            var outcomeService = new OutcomeServiceClient(outcomeBinding, outcomeEndpoint);

            var balanceEndpoint = new EndpointAddress(string.Format(balanceAddressFormat, host));
            var balanceBinding = new BasicHttpBinding { MaxReceivedMessageSize = 100000 };
            var balanceService = new BalanceServiceClient(balanceBinding, balanceEndpoint);

            var statisticsEndpoint = new EndpointAddress(string.Format(statisticsAddressFormat, host));
            var statisticsBinding = new BasicHttpBinding { MaxReceivedMessageSize = 100000 };
            var statisticsService = new StatisticsServiceClient(statisticsBinding, statisticsEndpoint);

            container.Register(Component.For<IncomeServiceClient>().Instance(incomeService));
            container.Register(Component.For<OutcomeServiceClient>().Instance(outcomeService));
            container.Register(Component.For<BalanceServiceClient>().Instance(balanceService));
            container.Register(Component.For<StatisticsServiceClient>().Instance(statisticsService));
        }
    }
}